﻿using KwaiXiaoDian.Data;
using KwaiXiaoDian.Entity.DO;
using KwaiXiaoDian.Entity.VO.User;
using KwaiXiaoDian.IDao;
using SSharing.Frameworks.NetCore.Common.Extends;
using SSharing.Frameworks.NetCore.Common.Results;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KwaiXiaoDian.Dao
{
    /// <summary>
    /// 用户dao
    /// </summary>
    public class UserDao : BaseDao<TUser>, IUserDao
    {
        private readonly KwaiXiaoDianDbContext _kwaiXiaoDianDbContext;

        public UserDao(KwaiXiaoDianDbContext kwaiXiaoDianDbContext) : base(kwaiXiaoDianDbContext)
        {
            _kwaiXiaoDianDbContext = kwaiXiaoDianDbContext;
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IPagingResult<TUser> GetPagingUsers(GetPagingUsersRequest request)
        {
            IPagingResult<TUser> pagingResult = new PagingResult<TUser>();

            var query = _kwaiXiaoDianDbContext.TUser.AsQueryable();

            //查询条件
            if (!request.Status.IsNullOrEmpty())
            {
                query = query.Where(p => p.IsEnabled == request.Status);
            }
            if (!request.Role.IsNullOrEmpty())
            {
                query = query.Where(p => p.Role == request.Role);
            }
            if (!request.UserId.IsNullOrEmpty())
            {
                var userId = request.UserId.Trim();
                query = query.Where(p => p.PhoneNumber.Contains(userId));
            }
            if (!request.Email.IsNullOrEmpty())
            {
                var email = request.Email.Trim();
                query = query.Where(p => p.Email.Contains(email));
            }

            //默认按Dns服务商排序
            query = query.OrderByDescending(p => p.CreatedTime);

            //分页
            return query.PagingWithNoLock(request.Page, request.Limit);
        }



    }
}
